package com.yyk.yykywd.mvvm.model

import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseResponse
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.config.HttpUrl
import com.yyk.yykywd.http.ApiService
import com.yyk.yykywd.mvvm.entity.FollowTypeBean
import com.yyk.yykywd.mvvm.entity.MaterialBean
import com.yyk.yykywd.util.UploadUtil
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.io.File
import java.util.*

/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */
class MaterialModel {

    fun uploadContractImg(type: String, img: String, mListener: ((Int, String) -> Unit)) {
        val file = File(img)
        val uploadUtil = UploadUtil()
        var requestUrl: String
        if (type == MATERIAL) {
            requestUrl = HttpUrl.ADD_MATERIAL
        } else {
            requestUrl = HttpUrl.ADD_CONTRACT_IMG
        }
        val newMap = getNewMap()
        putSign(newMap)
        uploadUtil.uploadFile(file, "img", requestUrl, newMap)
        uploadUtil.setOnUploadProcessListener(object : UploadUtil.OnUploadProcessListener {
            override fun onUploadDone(responseCode: Int, message: String) {
                mListener.invoke(responseCode, message)
            }

            override fun onUploadProcess(uploadSize: Int) {}
            override fun initUpload(fileSize: Int) {}
        })
    }

    fun delImg(img: String, getBase: GetBase) {
        val newMap = getNewMap()
        putImgPath(newMap, img)
        putSign(newMap)
        ApiService.getInstance().api.postDelImg(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun refund(pact_id: String, id: String, money: String, refund: String, refund_ok: String, cause: String, desc: String, getBase: GetBase) {
        val newMap = getNewMap()
        putKid(newMap, id)
        putPactId(newMap, pact_id)
        putUserId(newMap)
        putMoney(newMap, money)
        putRefund(newMap, refund)
        putRefundOk(newMap, refund_ok)
        putCause(newMap, cause)
        if (desc.isNotBlank()) {
            putDesc(newMap, desc)
        }
        putSign(newMap)
        ApiService.getInstance().api.postRefund(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun getMaterialList(page: String, num: String, getMaterialList: GetMaterialList) {
        val newMap = getNewMap()
        putUserId(newMap)
        putPage(newMap, page)
        putNum(newMap, num)
        putSign(newMap)
        ApiService.getInstance().api.postFodderList(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<MaterialBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: MaterialBean) {
                        getMaterialList.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getMaterialList.onError(e.toString())
                    }

                })
    }

    fun getPermission(getBase: GetBase) {
        val newMap = getNewMap()
        putUserId(newMap)
        putSign(newMap)
        ApiService.getInstance().api.checkInsertFodder(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun delMaterial(id: String, getBase: GetBase) {
        val newMap = getNewMap()
        putFodderId(newMap, id)
        putSign(newMap)
        ApiService.getInstance().api.postDelFodder(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun publishMaterial(content: String, pics: String, getBase: GetBase) {
        val newMap = getNewMap()
        putUserId(newMap)
        if (!content.isBlank()) {
            putContent(newMap, content)
        }
        if (!pics.isBlank()) {
            putPics(newMap, pics)
        }
        putSign(newMap)
        ApiService.getInstance().api.postInsertFodder(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun uploadContract(type: String,
                       id: String,
                       siteData: FollowTypeBean.SiteData,
                       order_code: String,
                       pact_img: String,
                       hos_sg_img: String,
                       medical_img: String,
                       front: String,
                       back: String,
                       flag: String,
                       handsel_price: String,
                       business_img: String,
                       agency_name: String,
                       getBase: GetBase) {
        val newMap = getNewMap()
        putUserId(newMap)
        putKid(newMap, id)
        putStatus(newMap, siteData.ids.toString())
        putOrderCode(newMap, order_code)
        putPactImg(newMap, pact_img)
        when (type) {
            FROM_HOSPITAL_UPLOAD -> {
                putType(newMap, "1")
                putHosImg(newMap, hos_sg_img)
                if (medical_img.isNotEmpty()) {
                    putMedicalImg(newMap, medical_img)
                }
            }
            FROM_AGENT_PERSONAL_UPLOAD, FROM_AGENT_COMPANY_UPLOAD,
            FROM_AGENT_PERSONAL_ADD, FROM_AGENT_COMPANY_ADD -> {
                if (type == FROM_AGENT_PERSONAL_UPLOAD || type == FROM_AGENT_PERSONAL_ADD) {
                    putType(newMap, "2")
                } else {
                    putType(newMap, "3")
                    if (type == FROM_AGENT_COMPANY_UPLOAD) {
                        putBusinessImg(newMap, business_img)
                    }
                }
                if (type == FROM_AGENT_PERSONAL_UPLOAD || type == FROM_AGENT_COMPANY_UPLOAD) {
                    putFront(newMap, front)
                    putDorsal(newMap, back)
                }
                putFlag(newMap, flag)
                putPrice(newMap, siteData.id)
                if (handsel_price.isNotBlank()) {
                    putHandselPrice(newMap, handsel_price)
                }
                putAgencyName(newMap, agency_name)
            }
        }

        putSign(newMap)
        val observable: Observable<BaseBean> = if (type == FROM_HOSPITAL_UPLOAD) {
            ApiService.getInstance().api.postHosPact(newMap)
        } else {
            ApiService.getInstance().api.postAgentPact(newMap)
        }
        observable
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun completionContract(id: String, pact_id: String, order_code: String, pact_img: String, getBase: GetBase) {
        val newMap = getNewMap()
        putKid(newMap, id)
//        putClientType(newMap,"2")
        putUserId(newMap)
        putPactId(newMap, pact_id)
        putOrderCode(newMap, order_code)
        putPactImg1(newMap, pact_img)
        putSign(newMap)
        ApiService.getInstance().api.postPact(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    interface GetMaterialList : BaseResponse {
        fun getInfo(info: MaterialBean)
    }
}